package node;

import javax.media.j3d.*;
import javax.vecmath.Tuple3d;

import com.sun.j3d.utils.geometry.Primitive;
import com.sun.j3d.utils.geometry.Sphere;
import component.AbstractComponent;

public class BallNode extends GeoNode {

	private Sphere ball = null;

	public BallNode(AbstractComponent parent, Tuple3d position, int status) {
		super(parent, position, status);
		createBall();
	}

	private void createBall() {
		ball = new Sphere(GeoData.BALL_RADIUS, Primitive.ENABLE_GEOMETRY_PICKING | Primitive.GENERATE_NORMALS | Primitive.ENABLE_APPEARANCE_MODIFY, getAppearance());
		getTranslationGroup().addChild(ball);
	}
	
	@Override
	public PickShape getPickShape() {
		throw new NodeException("Unsupported operation, PickShape for ball.");
	}
	
	public SceneGraphPath getSceneGraphPath() {
		Node[] nodes = new Node[2];
		
		nodes[0] = this;
		nodes[1] = ball;
		
		Transform3D localToWorld = new Transform3D();
		ball.getShape(Sphere.BODY).getLocalToVworld(localToWorld);
		
		SceneGraphPath path = new SceneGraphPath(this.getLocale(), nodes , ball.getShape(Sphere.BODY));
		path.setTransform(localToWorld);

		return path;
	}
	
	@Override
	public String toString() {
		return String.format("Ball %03d  %f, %f, %f", this.DEBUG_ID, this.getPos().x, this.getPos().y, this.getPos().z);
	}

	@Override
	protected Primitive getPrimitive() {
		return ball;
	}
	
	@Override
	public boolean intersect(GeoNode other) {
		if (this == other) return false;
		
		// Ball hack
		if (other instanceof BallNode) {
			return (this.getPos().distance(other.getPos()) < GeoData.BALL_RADIUS*2);
		} else {
			return other.intersect(this);
		}
	}

}
